import { getTDK,getHome } from "@/api/data";

export default {
  state:{
    customerServiceTelephone: '',
    logoLink:'',
    bannerList: [],
    consultTypeList: [],
    helpList: [],
    linkList: [],
    noticedList: [],
    tdK: [],
    standardTdk: {
      title: '190.vip',
      keywords: '190.vip',
      description: '190.vip'
    },
    defaultTdk: {
      title: '',
      keywords: '',
      description: ''
    }
  },
  mutations:{
    setCustomerServiceTelephone(state, data) {
      state.customerServiceTelephone = data;
    },
    setLogoLink(state,data){
      state.logoLink = data
    },
    setHomeData(state, data) {
      state.bannerList = data.bannerList;
      state.consultTypeList = data.consultTypeList;
      state.helpList = data.helpList;
      state.linkList = data.linkList;
      state.noticedList = data.noticedList;
    },
    setHelpList(state, data) {
      state.helpList = data;
    },
    setTDKData(state, data) {
      state.tdK = data;
    },
    setDefaultTdk(state, data) {
      state.defaultTdk.title = (data.title || '') + state.standardTdk.title;
      state.defaultTdk.keywords = data.keywords || state.standardTdk.title;
      state.defaultTdk.description = data.description || state.standardTdk.description;
    }
  },
  actions:{
    async commitHome({ commit }) {
      // 如果是客户端调用缓存storage
      // if (process.client) {
        const cacheData = JSON.parse(sessionStorage.getItem('setHomeData'));
        if (cacheData) {
          commit('setHomeData', cacheData);
        } else {
          const { code, data, message } = await getHome({ source: 0 });
          if (code === 200 && data) {
            commit('setHomeData', data);
            sessionStorage.setItem('setHomeData', JSON.stringify(data));
          } else {
            console.log('commitHome-error', message);
          }
        }
      // }
    },
    async commitTDK({ commit }) {
      try {
        const { code, data } = await getTDK();
        if (code === 200 && data) {
          commit('setTDKData', data);
        }
      } catch (error) {
        console.log(`tdk-error-${new Date().getTime()}:`, error);
      }
    }
  },
  getters:{
    getcustomerServiceTelephone: (state) => {
      return state.customerServiceTelephone;
    },
    getLogoLink:(state) => {
      return state.logoLink
    },
    getBannerList: (state) => {
      return state.bannerList;
    },
    getConsultTypeList: (state) => {
      return state.consultTypeList;
    },
    getHelpList: (state) => {
      if (state.helpList.length <= 6) {
        return state.helpList;
      }
      return state.helpList.slice(0, 6);
    },
    getLinkList: (state) => {
      return state.linkList;
    },
    getNoticedList: (state) => {
      return state.noticedList;
    },
    getTDK: (state) => {
      return state.tdK;
    },
    getHomeTDK: (state) => {
      const data = state.tdK.filter((o) => {
        return o.name === '首页';
      });
      return data.length > 0 ? data[0] : null;
    },
    getBookingTDK: (state) => {
      const data = state.tdK.filter((o) => {
        return o.name === '域名预订';
      });
      return data.length > 0 ? data[0] : null;
    },
    getBiddingTDK: (state) => {
      const data = state.tdK.filter((o) => {
        return o.name === '域名竞价';
      });
      return data.length > 0 ? data[0] : null;
    },
    getNewsTDK: (state) => {
      const data = state.tdK.filter((o) => {
        return o.name === '新闻资讯';
      });
      return data.length > 0 ? data[0] : null;
    },
    getDnregTDK: (state) => {
      const data = state.tdK.filter((o) => {
        return o.name === '域名注册';
      });
      return data.length > 0 ? data[0] : null;
    },
    getBuyoutpriceTDK: (state) => {
      const data = state.tdK.filter((o) => {
        return o.name === '一口价';
      });
      return data.length > 0 ? data[0] : null;
    },
    getDnpaTDK: (state) => {
      const data = state.tdK.filter((o) => {
        return o.name === '域名中介';
      });
      return data.length > 0 ? data[0] : null;
    },
    getRmregisterTDK: (state) => {
      const data = state.tdK.filter((o) => {
        return o.name === '商标服务';
      });
      return data.length > 0 ? data[0] : null;
    },
    getDefaultTdk: (state) => {
      return state.defaultTdk;
    }
  }
}
